import Snake from './Snake';
//创建一个食物类
class Food {
    snake : Snake;
    element: HTMLElement;
    constructor() {
        this.snake = new Snake();
        this.element = document.getElementById('food')!; //可能或取得是空节点，所以后面加上“!”
    }
    //获取食物坐标
    get X() {
        return this.element.offsetLeft
    }
    get Y() {
        return this.element.offsetTop
    }
    //修改食物的位置,随机出现的位置
    changePosition() {
        
        let top = Math.round(Math.random() * 29) * 10;
        let left = Math.round(Math.random() * 29) * 10;
        this.element.style.top = top + 'px'
        this.element.style.left = left + 'px'
        //防止食物出现的地方和蛇身体重合
        for (let i = 1; i < this.snake.bodies.length; i++){
            if ((this.element.offsetLeft === (this.snake.bodies[i] as HTMLElement).offsetLeft && this.element.offsetTop === (this.snake.bodies[i] as HTMLElement).offsetTop)) {
                this.changePosition()
            }
        }
    }
}
export default Food;